var app = angular.module("userManage", ['baseModule', 'ui.validate', 'ui.select', 'ui.bootstrap']);
app.controller("mainCtrl", function ($scope, base) {
    $scope.user = {};
    $scope.searchContent = "";
    $scope.data = {currentPage: 1};
    $scope.setPage = function (pageNo) {
        $scope.data.currentPage = pageNo;
    };
    $scope.pageChanged = function () {
        $scope.getUsers();
    };

    $scope.data.maxSize = 10;
    $scope.data.bigTotalItems = 20;
    $scope.data.bigCurrentPage = 1;
    $scope.data.pageSize = 10;
    $scope.getUsers = function () {//显示用户信息
        base.post("sample/getUserList", {pageSize: $scope.data.pageSize, currentPage: $scope.data.currentPage, searchContent: $scope.searchContent}).get(function (data) {
            if (data) {
                $scope.lists = data.list;
                $scope.data.bigTotalItems = data.totalRow;
                for (var i in  $scope.lists) {
                    if ($scope.lists[i].SEND_CENTER_STATUS == 0) {

                        $scope.lists[i].SEND_CENTER_STATUS_NAME = "未发送";

                    } else {
                        $scope.lists[i].SEND_CENTER_STATUS_NAME = "已发送";
                        $scope.lists[i].SEND_CENTER_STATUS_FLAG = true;
                    }

                    if ($scope.lists[i].RETURN_RESULT_FLAG == 0) {
                        $scope.lists[i].RETURN_RESULT_FLAG_NAME = "无结果";
                    } else {
                        $scope.lists[i].RETURN_RESULT_FLAG_NAME = "有结果";
                    }
                }
            }
        })
    }
    $scope.getUsers();
    $scope.getKsmc = function () {
        base.post("sample/getKsmc", {}).get(function (data) {
            $scope.ksmcs = data;
            $scope.ksed = "输血科";
        });
    }
    $scope.getSampleType = function () {
        base.post("sample/getSampleType", {}).get(function (data) {
            $scope.ybs = data;
            $scope.ybed = "血浆";
        });
    }
    $scope.getXMZT = function () {
        base.post("sample/getXMZT", {}).get(function (data) {
            $scope.xms = data;
            $scope.xmed = "乙肝五项";
        });
    }
    $scope.sexs = [{nan: "男"}, {nan: "女"}];
    $scope.lxs = [{lx: "住院"}, {lx: "门诊"}];
    $scope.init = function () {
        $scope.getSampleType();
        $scope.getKsmc();
        $scope.getXMZT();
    }

    $scope.addUser = function () {//打开新增窗口
        $scope.title = "新增";
        $('#myModal').modal({
            show: true,
            backdrop: true
        })
        $scope.pk = null;
        $scope.name = null;
        $scope.phoneno = null;
        $scope.ch = null;
    };
    $scope.saveSample = function () {
        var sex = $("#sex").val();
        var lx = $("#lx").val();
        if ($scope.name == null) {
            steveConfrim("请输入姓名！");
        }
        if ($scope.pk) {
            base.post("sample/updateSample", {pk: $scope.pk, name: $scope.name, sex: $scope.sexed, birth: $scope.date, brpk: $scope.brpk,
                phone: $scope.phoneno, ch: $scope.ch, lx: $scope.lxed, ksmc: $scope.ksed, yb: $scope.ybed, xmm: $scope.xmed
            }).get(function (data) {
                if (data == "true") {
                    steveAl("修改成功!", 1000);
                    $("#myModal").modal("hide");
                    $scope.getUsers();
                } else {
                    steveAl("修改失败!", 1000);
                }
            });


        } else {
            base.post("sample/addSample", {name: $scope.name, sex: $scope.sexed, birth: $scope.date, phone: $scope.phoneno, ch: $scope.ch,
                lx: $scope.lxed, ksmc: $scope.ksed, yb: $scope.ybed, xmid: $scope.xmed
            }).get(function (data) {
                if (data == 'true') {
                    steveAl("添加成功", 1000);
                    $("#myModal").modal("hide");
                    $scope.getUsers();
                } else {
                    steveAl("保存失败!", 1000);
                }
            });
        }
    }
    $scope.OpenUpdateModal = function (item) {
        $scope.title = "修改";
        $('#myModal').modal({
            show: true,
            backdrop: true
        })
        $scope.pk = item.PK;
        $scope.brpk = item.brpk;
        $scope.name = item.PATIENTNAME;
        $scope.phoneno = item.PHONENO;
        $scope.ch = item.BED_NO;
        $scope.ksed = item.SECTION;
        $scope.ybed = item.SAMPLE;
        $scope.xmed = item.EXAMINAIM;
        $scope.sexed = item.SEX;
        $scope.lxed = item.STAYHOSPITALMODE;
        var date = new Date(item.BIRTHDAY).Format("yyyy-MM-dd");
        $('#datetimepicker').val(date);
    }

    $scope.deleteInfo = function (item) {
        steveConfrim("确定删除吗" + item.PATIENTNAME + "？", function () {
            base.post("sample/deleteInfo", {pk: item.PK, brpk: item.brpk}).get(function (data) {
                if (data == 'true') {
                    $scope.getUsers();
                } else {
                    steveAl("删除失败!", 1000);
                }

            });
        });
    };
    $scope.init();
    var pl = [];
    $scope.printTM = function (list) {
        pl.splice(0, pl.length);
        pl.push(list);
        localStorage.pl = JSON.stringify(pl);
        window.open("printTM.jsp");
    }
    $scope.checked = function () {
        if ($scope.checkAll == true) {
            for (var i = 0; i < $scope.lists.length; i++) {
                $scope.lists[i].check = true;
            }
        } else {
            for (var i = 0; i < $scope.lists.length; i++) {
                $scope.lists[i].check = false;
            }
        }
    }

    $scope.plPrint = function () {
        var flat = false;
        pl.splice(0, pl.length);
        for (var i in $scope.lists) {
            if ($scope.lists[i].check == true) {
                pl.push($scope.lists[i]);
                flat = true;
            }
        }
        if (flat) {
            localStorage.pl = JSON.stringify(pl);
            window.open("printTM.jsp");

        }
    }

    $scope.sendCenter = function (item) {
        base.post("sample/sendToCenter", {pk: item.PK}).get(function (data) {
            if (data == 'true') {
                steveAl("发送成功", 1000);
            } else {
                steveAl("发送失败", 1000);
            }
        })
    }


    $scope.printRpt = function (item) {

        window.open("sample/printRpt?barcode="+item.BARCODE + "&print=true");
    }
    $scope.printRpt = function (item) {

        window.open("sample/printRpt?barcode="+item.BARCODE + "&print=false");
    }



    $('#datetimepicker').datetimepicker({
        language: 'zh-CN',
        format: 'yyyy-mm-dd ',
        startView: 3,
        autoclose: true,
        minView: 2
    }).on('changeDate', function (ev) {
        $scope.date = ev.date.Format("yyyy-MM-dd");
    });

});

